package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void remove(Long id) {
        //1.判断当前分类下是否存在菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Dish::getCategoryId, id);
        Integer count = dishMapper.selectCount(queryWrapper);
        //2.如果存在，不能删除，提示错误信息
        if (count >0) {
            throw new CustomException("当前分类下，关联了菜品,不能直接删除");
        }
        //3.判断当前分类下是否存在套餐
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        Integer selectCount = setmealMapper.selectCount(lambdaQueryWrapper);
        //4.如果存在，不能删除，提示信息
        if (selectCount>0){
            throw new CustomException("当前分类下，关联了菜品,不能直接删除");
        }
        //5.删除分类数据
        this.removeById(id);
    }
}
